import 'dart:convert' show json;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:lazy_data_table/lazy_data_table.dart';
import 'base_model_asT.dart';

typedef HeaderBuilder = Widget Function(int);
typedef DataCellBuilder = Widget Function(int, int);

class BaseChartTableModel {
  /// 表头组件
  Widget tableHeaderWidget;

  /// 表格样式
  LazyDataTableTheme tableTheme;

  /// 数据文本颜色
  Color dataTextColor;

  /// 行
  List<dynamic> rows;

  /// 列
  List<String> columns;

  /// 行首builder
  HeaderBuilder rowHeaderBuilder;

  /// 列首builder
  HeaderBuilder columnHeaderBuilder;

  /// 数据格builder
  DataCellBuilder dataCellBuilder;

  /// 表格元素宽高
  LazyDataTableDimensions tableDimensions;

  /// 表格行数据的集合
  List<List<String>> rowDatas;

  BaseChartTableModel({
    @required this.tableHeaderWidget,
    @required this.rowDatas,
    @required this.tableDimensions,
    @required this.rows,
    @required this.columns,
    @required this.tableTheme,
    @required this.rowHeaderBuilder,
    @required this.columnHeaderBuilder,
    @required this.dataCellBuilder,
    this.dataTextColor = Colors.black,
  }) : assert(tableHeaderWidget != null &&
            rowDatas != null &&
            tableTheme != null &&
            rowHeaderBuilder != null &&
            columnHeaderBuilder != null &&
            dataCellBuilder != null &&
            tableDimensions != null &&
            rows != null &&
            columns != null);

  factory BaseChartTableModel.fromJson(Map<String, dynamic> jsonRes) {
    return BaseChartTableModel(
      tableHeaderWidget: asT<Widget>(jsonRes['tableHeaderWidget']),
      rows: asT<List<dynamic>>(jsonRes['rows']),
      columns: asT<List<String>>(jsonRes['columns']),
      rowDatas: asT<List<List<String>>>(jsonRes['rowDatas']),
      tableDimensions: asT<LazyDataTableDimensions>(jsonRes['tableDimensions']),
      tableTheme: asT<LazyDataTableTheme>(jsonRes['tableTheme']),
      dataTextColor: asT<Color>(jsonRes['dataTextColor']),
      rowHeaderBuilder: asT<HeaderBuilder>(jsonRes['rowHeaderBuilder']),
      columnHeaderBuilder: asT<HeaderBuilder>(jsonRes['columnHeaderBuilder']),
      dataCellBuilder: asT<DataCellBuilder>(jsonRes['dataCellBuilder']),
    );
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'tableHeaderWidget': tableHeaderWidget,
        'rows': rows,
        'tableDimensions': tableDimensions,
        'columns': columns,
        'rowDatas': rowDatas,
        'tableTheme': tableTheme,
        'dataTextColor': dataTextColor,
        'rowHeaderBuilder': rowHeaderBuilder,
        'columnHeaderBuilder': columnHeaderBuilder,
        'dataCellBuilder': dataCellBuilder
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
